package com.wing.cms.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.cms.domain.Artgroup;
import com.wing.cms.domain.Article;
import com.wing.cms.service.ArtgroupManager;
import com.wing.cms.service.ArticleManager;
import com.wing.cms.service.Article_groupManager;
import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.common.util.RequestUtils;
import com.wing.portal.aggregation.portletinfo.ArticlePortletInfo;

/**
 * 
 * 
 * @author djl
 * 
 */
 
@Controller
@RequestMapping(value = "/cms/article")
public class ArticleController {

	@Autowired
	private ArticleManager articleManager;
	@Autowired
	private Article_groupManager article_groupManager;
	@Autowired
	private ArtgroupManager artgroupManager;
	
	
	/**
	 * 获得列表数据
	 *  
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "/cms/article/articleIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String delete(@PathVariable String url) {
		return "/cms/article/"+url;
	}
		
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(String grpid,String s,int currentPage, String pageSize,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  articleManager.findPageList(currentPage, _iPageSize,siteId,grpid,s);
	}	
	
	@RequestMapping(value = "cmslist", method = RequestMethod.POST)
	public @ResponseBody PageBean cmslist(String grpid,String s,int currentPage, String pageSize,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		PageBean pb = articleManager.findPageList(currentPage, _iPageSize,siteId,grpid,s);
		
		for(int i=0;i<pb.getRecordList().size();i++){
			Map map = (Map)pb.getRecordList().get(i);
			String content = "";
			try {
				content = articleManager.findContentById((String) map
						.get("id"));
			} catch (IOException e) {
				e.printStackTrace();
			}
			String imgSrc = ArticlePortletInfo.parsingImgUrl(content);
			map.put("firtImgrSrc", imgSrc);
		}
		return pb;
	}
	
	/**
	 * 2011-11-14
	 * 获取文章列表，包含文章内容
	 * @param grpid
	 * @param s
	 * @param currentPage
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "cmsclist", method = RequestMethod.POST)
	public @ResponseBody PageBean cmsclist(String grpid,String s,int currentPage, String pageSize,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		PageBean pb = articleManager.findPageList(currentPage, _iPageSize,siteId,grpid,s);
		
		for(int i=0;i<pb.getRecordList().size();i++){
			Map map = (Map)pb.getRecordList().get(i);
			String content = "";
			try {
				content = articleManager.findContentById((String) map
						.get("id"));
			} catch (IOException e) {
				e.printStackTrace();
			}
			String imgSrc = ArticlePortletInfo.parsingImgUrl(content);
			map.put("content", content);
			map.put("firtImgrSrc", imgSrc);
		}
		return pb;
	}
	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "toForm/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable String id,String grpid, Model model,HttpServletRequest request) throws IOException {
		Article article = null;
		String artgroupNames="";
		String artgroupIds="";
		if(!id.equals("0")){ 
			article = articleManager.findById(id);
			List lst = article_groupManager.getArtgroupByArtId(id); 
			for(int i=0;i<lst.size();i++){
				Map map = (Map)lst.get(i);
				artgroupNames+=map.get("name")+",";
				artgroupIds+=map.get("id")+"|";
			}
		}else{
			article = new Article();
			Artgroup ag = artgroupManager.findById(grpid);
			if(ag!=null){
				artgroupNames=ag.getName();
				artgroupIds=grpid;
			}
		}
		model.addAttribute("article", article);
		model.addAttribute("grpIds", artgroupIds);
		model.addAttribute("grpNames", artgroupNames);
		return "/cms/article/articleForm";
	}
	
	/**
	 * 打开文章页面
	 * @param id
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "show/{id}", method = RequestMethod.GET)
	public  @ResponseBody Article show(@PathVariable String id, Model model,HttpServletRequest request) throws IOException {
		Article article = null;
		String artgroupNames="";
		String artgroupIds="";
		if(!id.equals("0")){
			article = articleManager.findById(id);
		}else{
			article = new Article();
		}
		return article;
	}
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public  @ResponseBody  String save(Article article,HttpServletRequest request) throws IOException {
		String userId = (String)request.getSession().getAttribute(Constants.SESSION_USER_ID);
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		String grpIds = request.getParameter("grpIds");
		if(article.getId() == null ||  "".equals(article.getId())){
			article.setCreate_user(userId);
			article.setSite_id(siteId);
			article.setCreate_time(new Date());
		}
		String sitePath = RequestUtils.getSitePath(request);
		articleManager.saveArticle(article,grpIds,sitePath);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	
	@RequestMapping(value = "delarticles", method = RequestMethod.POST)
	public @ResponseBody String delarticles(String[] chk,HttpServletRequest request) throws IOException {
		String sitePath = RequestUtils.getSitePath(request);
		articleManager.deleteArticles(chk,sitePath);
		return ""+chk.length;
	}
}

